/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.model.Command;

/**
 *
 * @author Eric Qian
 */
public class CommandStack {

    /**
     * 
     * @param size
     */
    public CommandStack(int size) {
        this.size = size;
        commands = new Command[size];
        pointer = 0;
    }

    /**
     * 
     * @return
     */
    public boolean empty() {
        if (commands[pointer] == null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 
     * @return
     */
    public Command peek() {
        return commands[pointer];
    }

    /**
     * 
     * @return
     */
    public Command pop() {
        Command aCommand = commands[pointer];
        commands[pointer] = null;
        pointer = (pointer + size - 1) % size;
        return aCommand;
    }

    /**
     * 
     * @param aCommand
     */
    public void push(Command aCommand) {
        pointer = (pointer + 1) % size;
        commands[pointer] = aCommand;
    }

    /**
     * 
     * @param aCommand
     * @return
     */
    public int search(Command aCommand) {
        for (int i = 0; i < size; i++) {
            if (commands[(pointer + size - i) % size] == null) {
                break;
            } else if(commands[(pointer + size - i) % size].equals(aCommand)){
                return i;
            }
        }
        return -1;
    }

    public int getSize() {
        return size;
    }
    private int pointer;
    private int size;
    private Command[] commands;
}
